package com.cwvs.controller.api;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cwvs.models.ResultInfo;
import com.cwvs.services.AppDepartmentService;
import com.cwvs.services.AppOrderService;
import com.cwvs.services.AppStoreService;
import com.cwvs.services.AppUserService;
import com.cwvs.utils.CommonUtils;

/**
 * 订单Controller
 * 
 * @author mickey
 * 
 */
@Controller
@RequestMapping("/api")
public class AppOrderController {
	@Resource
	private AppOrderService appOrderService;
	@Resource
	private AppUserService appUserService;
	@Resource
	private AppStoreService appStoreService;
	@Resource
	private AppDepartmentService appDepartmentService;

	/**
	 * 订单生成
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/submit_order")
	public @ResponseBody
	ResultInfo order(HttpServletRequest request, HttpServletResponse response) {
		ResultInfo res = new ResultInfo();
		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.length() != 0) {
					map.put(paramName.toUpperCase(), paramValue.replaceAll("[\\t\\n\\r]", ""));
				}
			}
		}
		String token = request.getParameter("token");
		if (StringUtils.isBlank(token)) {
			res.setMessage("无效的登录信息");
			return res;
		}
		res = appUserService.getUserInfo(token);
		if (!res.isSuccess()) {
			return res;
		}
		Map<String, Object> orderType = appOrderService
				.getOrderTypeByCode(MapUtils.getString(map, "ORDER_TYPE"));
		if (null == orderType) {
			res.setMessage("无效的订单类型");
			res.setSuccess(false);
			return res;
		}
		if (StringUtils.equalsIgnoreCase(MapUtils.getString(map, "ORDER_TYPE"),
				"YDDD")
				|| StringUtils.equalsIgnoreCase(
								MapUtils.getString(map, "ORDER_TYPE"), "GHDD")) {
			Map<String, Object> store = appStoreService
					.getStoreInfoById(MapUtils.getString(map, "STORE_ID"));
			if (null == store||store.isEmpty()) {
				res.setMessage("请先选择店铺");
				if(StringUtils.equalsIgnoreCase("GHDD", MapUtils.getString(map, "ORDER_TYPE"))){
					res.setMessage("请先选择医院");
				}
				res.setSuccess(false);
				return res;
			}
			if(StringUtils.equalsIgnoreCase("GHDD", MapUtils.getString(map, "ORDER_TYPE"))){
				Map<String,Object> department=appDepartmentService.getDepartmentInfo(MapUtils.getString(map, "DEPARTMENT_ID"));
				if(department.isEmpty()||MapUtils.getIntValue(department, "IS_ACTIVE")==0){
					res.setMessage("请先选择科室");
					res.setSuccess(false);
					return res;
				}
				if(!StringUtils.equalsIgnoreCase(MapUtils.getString(department, "STORE_ID"), MapUtils.getString(map, "STORE_ID"))){
					res.setMessage("科室与医院不匹配");
					res.setSuccess(false);
					return res;
				}
				Map<String,Object> person=appDepartmentService.getPersonInfo(MapUtils.getString(map, "PERSON_ID"));
				if(person.isEmpty()||MapUtils.getIntValue(person, "IS_ACTIVE")==0){
					res.setMessage("请选择专家");
					res.setSuccess(false);
					return res;
				}
				if(!StringUtils.equalsIgnoreCase(MapUtils.getString(person, "DEPARTMENT_ID"), MapUtils.getString(map, "DEPARTMENT_ID"))){
					res.setMessage("专家与科室不匹配");
					res.setSuccess(false);
					return res;
				}
			}
			if(StringUtils.equalsIgnoreCase(MapUtils.getString(map, "ORDER_TYPE"),
					"YDDD")){
				map.put("TITLE", MapUtils.getString(store,"NAME")+"桌位预定");
			}
//			if(StringUtils.equalsIgnoreCase(MapUtils.getString(map, "ORDER_TYPE"),
//					"WMDD")){
//				map.put("TITLE", MapUtils.getString(store,"NAME")+"外卖订单");
//			}
			if(StringUtils.equalsIgnoreCase(MapUtils.getString(map, "ORDER_TYPE"),
					"GHDD")){
				map.put("TITLE", MapUtils.getString(store,"NAME")+"挂号");
			}
		}
		// 身份证号加密
		if (StringUtils.isNotBlank(MapUtils.getString(map, "ID_CARD"))) {
			map.put("ID_CARD", CommonUtils.encrypt(
					MapUtils.getString(map, "ID_CARD"), "3x9f1298"));
		}
		map.put("TYPE_ID", MapUtils.getString(orderType, "ID"));
		map.put("TYPE_CODE", MapUtils.getString(orderType, "CODE"));
		map.put("TYPE_NAME", MapUtils.getString(orderType, "NAME"));
		map.put("USER_ID",
				((Map<String, Object>) res.getResult()).get("USER_ID"));
		if(StringUtils.isBlank(MapUtils.getString(map, "TITLE"))){
			map.put("TITLE", MapUtils.getString(orderType, "NAME"));
		}
		map = appOrderService.createOrder(map);
		res.setSuccess(true);
		res.setMessage("订单生成成功");
		Map<String,Object> orderInfo=new HashMap<String,Object>();
		orderInfo.put("ORDER_NO", MapUtils.getString(map, "ORDER_NO"));
		orderInfo.put("ID", MapUtils.getString(map, "ID"));
		res.setResult(orderInfo);
		return res;
	}

	/**
	 * 获取外卖类型
	 * 
	 * @return
	 */
	@RequestMapping("/take_out")
	public List<Map<String, Object>> takeOutList() {
		return appOrderService.takeOutList();
	}

	/**
	 * 订单列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/order_list")
	public @ResponseBody
	ResultInfo orderList(HttpServletRequest request,
			HttpServletResponse response) {
		String token = request.getParameter("token");
		String st = request.getParameter("status");
		String[] status=new String[]{};
		if(StringUtils.isNotBlank(st)){
			status=st.split(",");
		}
		String page = request.getParameter("page");
		ResultInfo res = appUserService.getUserInfo(token);
		if (!res.isSuccess()) {
			return res;
		}
		int cpage = 1;
		if (StringUtils.isNotBlank(page)) {
			cpage = Integer.parseInt(page);
		}
		Map<String, Object> info = (HashMap<String, Object>) res.getResult();
		res.setMessage("查询成功");
		res.setSuccess(true);
		res.setResult(appOrderService.getOrderListByUser(
				MapUtils.getString(info, "USER_ID"), status, cpage));
		return res;
	}
	/**
	 * 订单分类总数
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/order_count")
	public @ResponseBody
	ResultInfo orderCount(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String,Object> map=new HashMap<String,Object>();
		String token = request.getParameter("token");
		ResultInfo res = appUserService.getUserInfo(token);
		if (!res.isSuccess()) {
			return res;
		}
		Map<String, Object> info = (HashMap<String, Object>) res.getResult();
		map.put("undone",appOrderService.getOrderCountByStatus(MapUtils.getString(info, "USER_ID"), new String[]{"1","2","3"}));
		map.put("transact",appOrderService.getOrderCountByStatus(MapUtils.getString(info, "USER_ID"), new String[]{"4"}));
		map.put("done",appOrderService.getOrderCountByStatus(MapUtils.getString(info, "USER_ID"), new String[]{"5","6","7","90"}));
		res.setResult(map);
		res.setMessage("查询成功");
		res.setSuccess(true);
		return res;
	} 
	
	
	/**
	 * 订单分类总数
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/order_info")
	public @ResponseBody
	ResultInfo orderInfo(HttpServletRequest request,
			HttpServletResponse response) {
		String token = request.getParameter("token");
		ResultInfo res = appUserService.getUserInfo(token);
		if (!res.isSuccess()) {
			return res;
		}
		String id = request.getParameter("id");
		Map<String, Object> info = (HashMap<String, Object>) res.getResult();
		Map<String,Object> order=appOrderService.orderInfoById(MapUtils.getString(info, "USER_ID"), id);
		res.setResult(order);
		res.setMessage("查询成功");
		res.setSuccess(true);
		if(order.isEmpty()){
			res.setMessage("订单不存在");
			res.setSuccess(false);
		}
		return res;
	} 
	

}
